export default class ConverUtil{

    /**
     * 将数字转化成有数字+单位 {100=> 100     10000=>1万  100000000=>1亿}
     */
    public static ToString(cover: any) {
        let numToStr = cover.toString();
        if (numToStr) {
            if (numToStr.indexOf("E") >= 0 || numToStr.indexOf('e') >= 0) {
                let dotIndex = numToStr.split('e+'); // dot
                let length = dotIndex[1];
                let str = dotIndex[0].split("");
                let index = str.indexOf(".");
                let numData = dotIndex[0];
                if (index >= 0) {
                    length = length - (str.length - index - 1);
                    numData = dotIndex[0].replace('.', '');
                }
                let zero = "";
                for (let i = 0; i < length; i++) {
                    zero += "0";
                }
                numToStr = numData.toString() + zero;
            }
        }
        let powArr = [0, 4, 8, 12, 16, 20, 24];
        let unit = ["", "万", "亿", "兆", "京", "垓", "秭"];
        let arr = [];
        for (let i = 0; i < powArr.length; i++) {
            let front = 4 * (i + 1);
            let last = 4 * i;
            let newStr = numToStr;
            if (last == 0) {
                newStr = newStr.slice(-1 * front)
            }
            else {
                newStr = newStr.slice(-1 * front, -1 * last)
            }
            arr.push(newStr);
            if (numToStr.length <= front) {
                break;
            }
        }
        let writeStr = '';
        for (let i = arr.length - 1; i >= 0; i--) {
            let numState = arr.pop();
            if (numState == "0000") {
                continue;
            }
            writeStr += Number(numState) + unit[i];
        }
        return writeStr;

    } 
    
    private static unitArr(itemStr){
        let arr=itemStr
            .replace(/(\w*秭)(\w*)/, "$1,$2")
            .replace(/(\w*垓)(\w*)/, "$1,$2")
            .replace(/(\w*京)(\w*)/, "$1,$2")
            .replace(/(\w*兆)(\w*)/, "$1,$2")
            .replace(/(\w*亿)(\w*)/, "$1,$2")
            .replace(/(\w*万)(\w*)/, "$1,$2")
            .split(',')
            .filter(_item => {
                return _item && _item.trim();
            });
        return arr
    }

     //字符串边数字
    public static ToNumber(itemStr) {
        if(typeof itemStr==="number"){
            return itemStr;
        }
        let itemCount = 0;
        let arr = this.unitArr(itemStr)
        // 处理每个单位对应的值
        for (let subItem of arr) {
            let currValue = 0;
            let subArr = subItem.split('');
            let num = Number(subItem.match(/^[0-9]+/gi));
            let unit = subArr[subArr.length - 1];
            if (unit === '秭') {
                currValue = Math.pow(10, 24) * num
            } else if (unit === '垓') {
                //处理佰位
                currValue = Math.pow(10, 20) * num
            } else if (unit === '京') {
                // 处理仟位
                currValue = Math.pow(10, 16) * num
            }
            else if (unit === '兆') {
                // 处理仟位
                currValue = Math.pow(10, 12) * num
            }
            else if (unit === '亿') {
                // 处理仟位
                currValue = Math.pow(10, 8) * num
            }
            else if (unit === '万') {
                // 处理仟位
                currValue = Math.pow(10, 4) * num
            }
            else {
                // 处理两位
                currValue = (num);
            }
            itemCount += currValue;
        }
        return itemCount;
    }

    public static showShortCoin(str:string,length=3){
        let arr = this.unitArr(str);
        if(arr.length<=length){
            return str;
        }
        let outStr='';
        for(let i=0;i<length&&i<arr.length;i++){
            outStr+=arr[i];
        }
        return outStr
    }

}
